import 'package:dio/dio.dart';
import 'package:flower/config/service_url.dart';
import 'package:flower/model/coupon_model.dart';
import 'package:flower/utils/http_util.dart';
import 'package:flutter/material.dart';

/* 优惠券页面 */
class CouponList extends StatefulWidget {
  const CouponList({super.key});

  @override
  State<CouponList> createState() => _CouponListState();
}

class _CouponListState extends State<CouponList> with SingleTickerProviderStateMixin {
  final List<String> couponStatus = ['未使用', '已使用', '已过期'];

  List<CouponData> couponListData = [];
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: couponStatus.length, vsync: this);
    _tabController.addListener(() {
      if (_tabController.indexIsChanging) {
        queryCouponList(_tabController.index);
      }
    });
    queryCouponList(0);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void queryCouponList(int status) async {
    Map<String, dynamic> map = <String, dynamic>{};
    map["status"] = status;
    Response result = await HttpUtil.get(listByStatus, queryParameters: map);
    CouponModel couponModel = CouponModel.fromJson(result.data);
    setState(() {
      couponListData = couponModel.data;
    });
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: couponStatus.length,
      child: Scaffold(
        appBar: AppBar(
          title: const Text(' 优惠券列表 '),
          titleTextStyle: const TextStyle(fontSize: 16, color: Colors.black),
          centerTitle: true,
          bottom: TabBar(
            controller: _tabController,
            indicatorColor:
                Color(int.parse('fa436a', radix: 16)).withAlpha(255),
            labelColor: Color(int.parse('fa436a', radix: 16)).withAlpha(255),
            tabs: couponStatus.map((status) => Tab(text: status)).toList(),
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: couponStatus.map((status) {
            return CouponListInfo(
              status: status,
              couponListData: couponListData,
            );
          }).toList(),
        ),
      ),
    );
  }
}

class CouponListInfo extends StatelessWidget {
  final String status;
  final List<CouponData> couponListData;

  const CouponListInfo({
    super.key,
    required this.status,
    required this.couponListData,
  });

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: couponListData.length,
        itemBuilder: (context, index) {
          CouponData couponData = couponListData[index];
          return Container(
              padding: const EdgeInsets.all(15),
              decoration: BoxDecoration(
                  border: Border(
                      bottom: BorderSide(
                          width: 5,
                          color: Color(int.parse('f5f5f5', radix: 16))
                              .withAlpha(255)))),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(couponData.name,
                                style: TextStyle(
                                    fontSize: 16,
                                    color: Color(int.parse('303133', radix: 16))
                                        .withAlpha(255))),
                            Text("有效期至${couponData.endTime}",
                                style: TextStyle(
                                    fontSize: 12,
                                    color: Color(int.parse('909399', radix: 16))
                                        .withAlpha(255))),
                          ],
                        ),
                      ),
                      Column(
                        children: [
                          Row(
                            children: [
                              Text("￥",
                                  style: TextStyle(
                                      fontSize: 17,
                                      color:
                                          Color(int.parse('fa436a', radix: 16))
                                              .withAlpha(255))),
                              Text(couponData.discount.toString(),
                                  style: TextStyle(
                                      fontSize: 22,
                                      color:
                                          Color(int.parse('fa436a', radix: 16))
                                              .withAlpha(255))),
                            ],
                          ),
                          Text("满${couponData.threshold}可用",
                              style: TextStyle(
                                  fontSize: 13,
                                  color: Color(int.parse('707070', radix: 16))
                                      .withAlpha(255))),
                        ],
                      )
                    ],
                  ),
                  Container(
                    height: 5,
                  ),
                  Text("全场可用",
                      style: TextStyle(
                          fontSize: 12,
                          color: Color(int.parse('909399', radix: 16))
                              .withAlpha(255))),
                ],
              ));
        });
  }
}
